﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace EvoGame
{
    class SpriteSheet
    {
        string path;
        int size;
        public Texture2D tex;

        /// <summary>
        /// Initialization for the sprite sheet
        /// </summary>
        /// <param name="path">The path to the sprite sheet</param>
        /// <param name="size">The size in pixels of a sprite on the sprite sheet</param>
        public SpriteSheet(string path, int size)
        {
            this.path = path;
            this.size = size;
        }

        /// <summary>
        /// Here the texture is loaded into memory
        /// </summary>
        /// <param name="content">An Object Reference for the Content Object in the project</param>
        public void load(ContentManager content)
        {
            tex = content.Load<Texture2D>(path);
        }

        /// <summary>
        /// A renderrer for a sprite designated bu the x and y on the SpriteSheet
        /// </summary>
        /// <param name="sb">An Object Reference for the SpriteBatch Object in the project</param>
        /// <param name="pos">A Vector2 for the position of the sprite on the screen</param>
        /// <param name="origin">A Vector2 for the origin position on the sprite</param>
        /// <param name="x">The x position on the SpriteSheet</param>
        /// <param name="y">The y position on the SpriteSheet</param>
        /// <param name="scale">The value of scaling of the Sprite</param>
        /// <param name="rotation">The value of rotation in radiants</param>
        /// <param name="depth">The depth of the sprite 0 meening in the front, 1 meening in the back</param>
        public void render(SpriteBatch sb, Vector2 pos, Vector2 origin, int x, int y, float scale, float rotation, int depth)
        {
            sb.Draw(tex, pos, new Rectangle(x * size, y * size, size, size), Color.White, rotation, origin, scale, SpriteEffects.None, depth);
        }

        public Texture2D Crop(Rectangle area)
        {
            Texture2D cropped = new Texture2D(tex.GraphicsDevice, area.Width, area.Height);
            Color[] data = new Color[tex.Width * tex.Height];
            Color[] cropData = new Color[cropped.Width * cropped.Height];

            tex.GetData<Color>(data);

            int index = 0;
            for (int y = area.Y; y < area.Y + area.Height; y++)
            {
                for (int x = area.X; x < area.X + area.Width; x++)
                {
                    cropData[index] = data[x + (y * tex.Width)];
                    index++;
                }
            }

            cropped.SetData<Color>(cropData);

            return cropped;
        }
    }
}
